package typeInfo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class SimpleDynamicProxy {

    static void consumer(Interface it){
        it.doSth();
        it.doElse("dsfsaf");
    }
    public static void main(String[] args) {
        RealObject realObject = new RealObject();
        consumer(realObject);
        System.out.println("--------");

        Interface proxyInstance = (Interface)Proxy.newProxyInstance(Interface.class.getClassLoader(), new Class[]{Interface.class},
                new DynamicProxyHandler(realObject));
        consumer(proxyInstance);

    }

}
class DynamicProxyHandler implements InvocationHandler{
    private Object proxied;
    public DynamicProxyHandler(Object proxied) {
        this.proxied = proxied;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("Proxy: getClass():" + proxy.getClass() + "getMethod():" + method + "getArgs:" + args);

        if (args != null) {
            for (Object arg : args) {
                System.out.print(" "+arg);
            }
        }

        return method.invoke(proxied, args);
    }

}



interface Interface{
    void doSth();
    void doElse(String ars);
}

class RealObject implements Interface
{

    @Override
    public void doSth() {
        System.out.println("realObj do sth");
    }

    @Override
    public void doElse(String ars) {

        System.out.println("realObj do sElse");

    }
}